<?php

declare(strict_types=1);

namespace app\controller;

use app\model\admin\Areas;
use think\facade\Filesystem;
use think\facade\Validate;
use think\Request;

class CommonController
{
    // 获取所有的省份
    public function getProvinceList(Request $request)
    {
        $list = Areas::where('parent_id', 0)->select();
        return success($list);
    }
    // 获取所有的市
    public function getCityList(Request $request)
    {
        $pid = input("pid");
        if (!$pid) {
            return error("参数错误");
        }
        $list = Areas::whereIn('parent_id', $pid)->select();
        return success($list);
    }
    // 上传支付文件
    public function uploadPayfile(Request $request)
    {
        // var_dump(request()->file('file'));
        // $files = request()->file();
        // // 最大5M
        // $fileSize = 1024 * 1024 * 5;
        // $validate = Validate::rule([
        //     "file" => 'fileSize:' . $fileSize . '|fileExt:jpg,png,jpeg'
        // ]);
        // if (!$validate->check($files)) {
        //     return error($validate->getError());
        // }
        // $saveName = \think\facade\Filesystem::disk('public')->putFile('uploads', $files['file']);
        // return success('storage/' . $saveName);

        $files = request()->file();
        $fileSize = 1024 * 5;  //支付文件最大5k
        $validate = validate([
            "file" => 'fileSize:' . $fileSize . '|fileExt:pem'
        ]);
        if (!$validate->check($files)) {
            return error($validate->getError());
        }
        $type = $request['type'];
        // 生成新的名字
        if ($type == 'apiclient_key') {
            $saveName = \think\facade\Filesystem::disk('payfile')->putFile('apiclient_key', $files['file']);

            // $file->storePubliclyAs('uploads/pay/apiclient_key', $newFilename, ['disk' => 'public']);
        } else if ($type == 'apiclient_cert') {
            $saveName = \think\facade\Filesystem::disk('payfile')->putFile('apiclient_cert', $files['file']);
        } else {
            return error('参数错误');
        }
        // 支付证书路径返回给前端,无法访问
        return success($saveName);
    }
    // 上传图片
    public function uploadFile(Request $request)
    {
        // var_dump(request()->file('file'));
        $files = request()->file();
        // 最大5M
        $fileSize = 1024 * 1024 * 5;
        $validate = Validate::rule([
            "file" => 'fileSize:' . $fileSize . '|fileExt:jpg,png,jpeg'
        ]);
        if (!$validate->check($files)) {
            return error($validate->getError());
        }
        $saveName = \think\facade\Filesystem::disk('public')->putFile('uploads', $files['file']);
        return success('storage/' . $saveName);
    }
    // 小程序上传头像图片
    public function miniAvatarUploadFile(Request $request)
    {
        // var_dump(request()->file('file'));
        $files = request()->file();
        // 最大5M
        $fileSize = 1024 * 1024 * 5;
        $validate = Validate::rule([
            "file" => 'fileSize:' . $fileSize . '|fileExt:jpg,png,jpeg'
        ]);
        if (!$validate->check($files)) {
            return error($validate->getError());
        }
        $saveName = \think\facade\Filesystem::disk('public')->putFile('uploads/avatar', $files['file']);
        return success('storage/' . $saveName);
    }
    // 小程序上传头像图片
    public function miniUploadFile(Request $request)
    {
        // var_dump(request()->file('file'));
        $files = request()->file();
        // 最大5M
        $fileSize = 1024 * 1024 * 5;
        $validate = Validate::rule([
            "file" => 'fileSize:' . $fileSize . '|fileExt:jpg,png,jpeg,webp'
        ]);
        if (!$validate->check($files)) {
            return error($validate->getError());
        }
        $saveName = \think\facade\Filesystem::disk('public')->putFile('uploads', $files['file']);
        return success('storage/' . $saveName);
    }
    // 上传图片
    public function uploadFileByEdit(Request $request)
    {
        // var_dump(request()->file('wangfile'));
        // 获取上传文件
        $files = request()->file();
        // 最大5M
        $fileSize = 1024 * 1024 * 5;
        $validate = Validate::rule([
            "wangfile" => 'fileSize:' . $fileSize . '|fileExt:jpg,png,jpeg'
        ]);
        if (!$validate->check($files)) {
            return error($validate->getError());
        }
        $saveName = \think\facade\Filesystem::disk('public')->putFile('wangUploads', $files['wangfile']);
        // {
        //     "errno": 0, // 注意：值是数字，不能是字符串
        //     "data": {
        //         "url": "xxx", // 图片 src ，必须
        //         "alt": "yyy", // 图片描述文字，非必须
        //         "href": "zzz" // 图片的链接，非必须
        //     }
        // }
        return json([
            'errno' => 0,
            'data' => [
                "url" => env('APP_URL') . "/storage/" . $saveName,
                "alt" => "",
                "href" => env('APP_URL') . "/storage/" . $saveName,
            ],
        ], 200, [], []);
    }
    public function uploadFile2(Request $request)
    {
        $files = request()->file();
        // 最大5M
        $fileSize = 1024 * 1024 * 5;
        $validate = Validate::rule([
            "file" => 'fileSize:' . $fileSize . '|fileExt:jpg,png,jpeg'
        ]);
        if (!$validate->check($files)) {
            return error($validate->getError());
        }
        $saveName = \think\facade\Filesystem::disk('public')->putFile('uploads', $files['file']);
        return success(['url' => 'storage/' . $saveName]);
    }
}
